/*
 * @Description: 格式转换
 * @Author: Zzt
 * @Date: 2022-09-19 11:08:58
 * @LastEditTime: 2022-09-21 10:38:31
 */

import { Button, Modal, notification } from 'antd';
import { useEffect, useMemo, useState } from 'react';
import { RetweetOutlined } from '@ant-design/icons';
import { formatConversion } from '@/api/archives/operate';
import { useLevelTableContext } from '../../contexts/LevelTableContext';

const FormatConversion = () => {
  const { baseInfo, selectItems } = useLevelTableContext();

  // 弹窗展示状态
  const [visible, setVisible] = useState(false);

  const handleOk = () => {
    const params = {
      commonParams: { dbName: baseInfo.dbName },
      coordinate: { atCode: baseInfo.atCode, atType: baseInfo.atType, level: baseInfo.level },
      entryIdList: selectItems
    };
    formatConversion(params).then((res: any) => {
      setVisible(false);
      notification.open({
        message: '格式转换',
        description: (
          <>
            <RetweetOutlined />
            文件转换已开始
          </>
        ),
        onClick: () => {}
      });
    });
  };
  const handleCancel = () => {
    setVisible(false);
  };
  return (
    <>
      <Button
        disabled={selectItems.length === 0}
        onClick={() => {
          setVisible(true);
        }}
      >
        格式转换
      </Button>
      {useMemo(
        () => (
          <Modal
            visible={visible}
            title="格式转换"
            onCancel={handleCancel}
            footer={[
              <Button key="取消" onClick={handleCancel}>
                取消
              </Button>,
              <Button key="submit" type="primary" onClick={handleOk}>
                转换
              </Button>
            ]}
          >
            <b>确定要转换吗？</b>
            <p>文件转换结果请在通知内查看</p>
          </Modal>
        ),
        [visible]
      )}
    </>
  );
};
export default FormatConversion;
